package com.app.service;

import com.app.model.main.LoginRecord;
import com.app.repository.interfaces.LoginRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Lichaojie on 2017/5/4. It's a beautiful day.
 */
@Service
public class LoginRecordService {

    @Autowired
    private LoginRecordMapper loginRecordMapper;


    /**
     * 获取userId对应的用户days天内的登录天数
     * @param days
     * @param userId
     * @return
     */
    public int getLoginTimesByDay(int days,long userId){
        long time2 = System.currentTimeMillis();
        long time1 = time2 - 24 * 60 * 60 * 1000 * days;

        List<LoginRecord> loginRecordList = loginRecordMapper.getListByUserIdBetween(userId,Long.toString(time1),Long.toString(time2));
        Set<Integer> timeDifference = new HashSet<>();
        if(loginRecordList != null){
            for (LoginRecord loginRecord : loginRecordList){
                long time = Long.parseLong(loginRecord.getLoginTime());
                long difference = time2 - time;
                timeDifference.add((int)(difference / (24 * 60 * 60 * 1000)));
            }
        }
        return timeDifference.size();
    }
}
